﻿@using LuDragon.Data.Entities;
@{
    ViewBag.Title = "Navigation";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
    var data = ViewBag.Data as List<Sys_Navigation>;
}

<span class="layui-breadcrumb">
    <a>系统设置</a>
    <a>菜单导航</a>
    <a><cite>列表</cite></a>
</span>
<hr />
<div class="layui-btn-container">
    <button class="layui-btn" onclick="location.href='NavigationEdit'"><i class="layui-icon">&#xe654;</i>新增</button>
    <button class="layui-btn layui-btn-normal" onclick="SaveSort();"><i class="layui-icon">&#xe618;</i>保存排序</button>
</div>
<div class="table-list layui-form">
    <table class="layui-table layui-list-table">
        <thead>
            <tr>
                <th>导航名称</th>
                <th>父级导航</th>
                <th>链接地址</th>
                <th>是否启用</th>
                <th>排序</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < data.Count; i++)
            {
                <tr>
                    <td><span class="layui-table-bd">@data[i].NavName</span></td>
                    <td>
                        @if (data[i].ParentId == 0)
                        {
                            <span class="layui-table-bd">无</span>
                        }
                        else
                        {
                            for (int j = 0; j < data.Count; j++)
                            {
                                if (data[i].ParentId == data[j].NavId)
                                {
                                    <span class="layui-table-bd">@data[j].NavName</span>
                                    break;
                                }
                            }
                        }
                    </td>
                    <td><span class="layui-table-bd">@data[i].Url</span></td>
                    <td>
                        @if (data[i].IsSystem)//系统菜单不能禁用
                        {
                            <span class="layui-table-bd">
                                <input type="checkbox" name="switch" checked="@data[i].Enabled" lay-skin="switch" lay-text="是|否" disabled>
                            </span>
                        }
                        else
                        {
                            <span class="layui-table-bd" onclick="ChangeEnabled(this,@data[i].NavId)">
                                <input type="checkbox" name="switch" checked="@data[i].Enabled" lay-skin="switch" lay-text="是|否">
                            </span>
                        }
                    </td>
                    <td width="5%">
                        <div class="layui-input-inline layui-table-bd">
                            <input type="number" value="@data[i].Sort" lay-verify="required" autocomplete="off" class="layui-input" data-id="@data[i].NavId" name="Sort">
                        </div>
                    </td>
                    <td class="layui-table-btn">
                        <button title="编辑" class="layui-btn layui-btn-normal layui-btn-small" onclick="location.href='NavigationEdit?id=@data[i].NavId'"><i class="layui-icon">&#xe642;</i></button>
                        @if (!data[i].IsSystem)//系统菜单不能删除
                        {
                            <button title="删除" class="layui-btn layui-btn-danger layui-btn-small" onclick="Del(@data[i].NavId);"><i class="layui-icon">&#xe640;</i></button>
                        }
                    </td>
                </tr>
            }
        </tbody>
    </table>
</div>
<script>
    //切换启用状态
    function ChangeEnabled(dom, navId) {
        var checkbox = dom.getElementsByTagName('input')[0];
        var checked = checkbox.checked;
        $.post("ChangeEnabled", { "navId": navId, "enabled": checked }, function (result) {
            var data = JSON.parse(result);
            if (data.State != "success") {
                checkbox.checked = false;
            }
        })
    };
    //删除
    function Del(id) {
        dialog.dialog("删除操作不可恢复，您确认要删除吗？", function () {
            $.post("Del", { "id": id }, function (result) {
                var data = JSON.parse(result);
                if (data.Code == 200) {
                    location.reload();
                } else {
                    dialog.prompt("操作失败，请稍后重试。");
                }
            })
        });
    }
    //保存排序
    function SaveSort() {
        var ids = "", sorts = "";
        var cols = $("input[name=Sort]");
        for (var i = 0; i < cols.length; i++) {
            ids += cols[i].dataset.id + ",";
            sorts += cols[i].value + ",";
        }
        $.post("SaveSort", { "ids": ids, "sorts": sorts }, function (result) {
            var data = JSON.parse(result);
            if (data.Code == 200) {
                location.reload();
            } else {
                dialog.prompt("操作失败，请稍后重试。");
            }
        })
    }

</script>
